package com.anxin.act.task.service;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.anxin.act.task.entity.ViewHiTask;
import com.anxin.act.task.entity.ViewRuTask;
import com.anxin.act.utils.ActProcessUtils;
import com.anxin.common.utils.LogUtils;
import com.anxin.common.utils.StringUtils;
import com.anxin.common.utils.sys.UserUtils;
import com.anxin.framework.security.entity.SysTempToken;
import com.anxin.framework.security.utils.SysTempTokenUtils;
import com.anxin.sys.file.kkFileView.hutool.URLEncoder;
import com.anxin.sys.system.entity.SysUser;
import com.anxin.sys.system.utils.ConfigUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * 待办对接
 */
@Service
public class ActDockingSendService {
    @Autowired
    private ActDockingSendFactory actDockingSendFactory;
    @Value("${taskunified.localUrl}")
    private String localUrl;

    /**
     * 对接发送待办
     * @param viewRuTask
     * @param userId
     * @param userName
     */
    public void sendTodoTask(ViewRuTask viewRuTask,String userId,String userName){
        ActDockingSend actDockingSend = actDockingSendFactory.get();
//        if (actDockingSend != null){
        JSONObject jsonObject = new JSONObject();
        String taskId = viewRuTask.getTaskId();
        String actTitle = new URLEncoder().encode(viewRuTask.getTitle(),Charset.defaultCharset());
        SysTempToken sysTempToken = SysTempTokenUtils.saveToken(userId, viewRuTask.getId(), "/act/task/processingTask?taskId=" + viewRuTask.getId());
            SysTempToken tempToken = SysTempTokenUtils.saveToken(userId, viewRuTask.getId() + "_dingtalk", "/actTaskForm?id="+taskId + "&title=" + actTitle);
            // 准备发送数据

        String cropId = ConfigUtils.get("dd.corpId");
        SysUser user = UserUtils.getUser(userId);
        List<String> userIds = new ArrayList<>();
        if (user != null) {
            if (StringUtils.isNotBlank(user.getJobId())) {
                userIds.add(user.getJobId());
            }
        }
        if (cropId != null && !StringUtils.equals("",cropId) && userIds.size() > 0) {
            String title = viewRuTask.getTitle();
            String url = tempToken.getUseUrl();
            String webUrl = ConfigUtils.get("dd.taskUrl");
            if (StringUtils.isNotBlank(webUrl)) {
                URLEncoder encoder = new URLEncoder();
                url = webUrl + url;
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append("#### ");
                stringBuilder.append(title);
                stringBuilder.append("  \n  ");
                stringBuilder.append("申请人：");
                stringBuilder.append(ActProcessUtils.getProcessVar(viewRuTask.getActId(),"applyNickName"));
                stringBuilder.append("  \n  ");
                stringBuilder.append("申请时间：");
                stringBuilder.append(DateUtil.format(viewRuTask.getStartTime(),"yyyy-MM-dd HH:mm:ss"));
//                dingTalkUtil.sendDingTalkCard(userIds, title, stringBuilder.toString(),"前往审批",url,"0",new JSONArray());
            }
        }
//        }
    }

    /**
     * 对接发送待办
     * @param viewRuTask
     * @param userId
     * @param userName
     */
    public SysTempToken sendTodoTask(ViewRuTask viewRuTask,String userId,String userName,String ownerId){
        LogUtils.info("④ 准备推送统一待办");
        ActDockingSend actDockingSend = actDockingSendFactory.get();
        if (actDockingSend != null){
            LogUtils.info("⑤ 准备推送统一待办");
            SysTempToken sysTempToken = SysTempTokenUtils.saveToken(ownerId, viewRuTask.getId(), localUrl+"/unifiedTask?taskId=" + viewRuTask.getId()+"&taskStatus=DB");
            LogUtils.info("⑥ 开始推送统一待办");
            actDockingSend.sendTodoTask(sysTempToken,viewRuTask,userId,userName);
            return sysTempToken;
        }
        return null;
    }

    /**
     * 对接删除待办
     * @param actId
     * @param taskId
     * @param userId
     * @param userName
     */
    public void delTodoTask(String actId,String taskId,String userId,String userName){
        ActDockingSend actDockingSend = actDockingSendFactory.get();
        if (actDockingSend != null){
            actDockingSend.delTodoTask(actId,taskId,userId,userName);
        }
    }

    /**
     * 对接发送已办
     * @param viewHiTask
     * @param userId
     * @param userName
     */
    public void sendHiTask(ViewHiTask viewHiTask,String userId,String userName){
        ActDockingSend actDockingSend = actDockingSendFactory.get();
        if (actDockingSend != null){
            actDockingSend.sendHiTask(viewHiTask,userId,userName);
        }
    }

    /**
     * 对接删除已办
     * @param viewHiTask
     * @param userId
     * @param userName
     */
    public void delHiTask(ViewHiTask viewHiTask,String userId,String userName){

    }

}
